import json
import time
import asyncio
import uvicorn
from fastapi import FastAPI, WebSocket


app = FastAPI()


async def test_wbs_path_connection(websocket: WebSocket):
    await websocket.accept()
    while True:
        await asyncio.sleep(1)
        data = [{'target_id': '6b72fab3-ee66-420e-8813-d690e5caaa5a', 'traffic': 30, 'unique_id': '40f29999-be70-4fba-8c4c-756248338fdf'},
                {'target_id': '6b72fab3-ee66-420e-8813-d690e5caaa5a', 'traffic': 55, 'unique_id': 'ffe5ced6-f847-4da6-8a9b-84590f0f89c3'}
                ]
        await websocket.send_text(json.dumps(data, ensure_ascii=False))

async def test_http_path_connection():
    print('aaa')
    await asyncio.sleep(10)
    return 'test http'


app.add_api_websocket_route(path='/test_websocket_path', endpoint=test_wbs_path_connection)
app.add_api_route(path='/test_http_path', endpoint=test_http_path_connection, methods=['GET'])


if __name__ == '__main__':
    uvicorn.run('http_and_websock:app', host='0.0.0.0', port=8001)
